package com.neusoft.nep.NEPM.controller;
import static com.neusoft.tools.FXMLUtils.switchToShow;
import static com.neusoft.tools.FileUtils.*;

import com.neusoft.nep.entity.Gridder;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FXMLUtils;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.stage.Modality;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.neusoft.nep.entity.nepsFeedback;
public class assignViewController{
    @FXML
    TextField txt_number,txt_name,txt_phonenumber,txt_date,txt_address,txt_content,txt_level;
    public nepsFeedback n;
    @FXML
    nepsFeedback nepsf;
    @FXML
    private Button btn_out,back;
    @FXML
    private TreeView treeview;
    @FXML
    private TreeItem To_AqiTrendTotalStatisView;
    @FXML
    private TreeItem To_AqiSeperatedStatisView;
    @FXML
    private TreeItem To_OtherStatisView;
    @FXML
    private TreeItem To_ProvinceDataTotalStatisView;
    @FXML
    TreeItem To_confirmDataView;
    @FXML
    TreeItem To_publicAqiDataList;
    @FXML
    List<nepsFeedback> lista=new ArrayList<>();
    @FXML
    List<nepsFeedback> listb=new ArrayList<>();
    @FXML
    List<nepsFeedback> listc=new ArrayList<>();
    @FXML
    List<nepsFeedback> listd=new ArrayList<>();
    @FXML
    List<nepsFeedback> liste=new ArrayList<>();

    @FXML
    public void initialize(){
        treeview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == To_AqiSeperatedStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiSeperatedStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_AqiTrendTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiTrendTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_OtherStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/OtherStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_ProvinceDataTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/ProvinceDataTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }

            }
            else if (newValue == To_confirmDataView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/confirmDataView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }else if (newValue == To_publicAqiDataList) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataList.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
        });
    }
    public void Log_out() throws IOException {
        FXMLUtils.switchToShow(btn_out,"NEPM/view/LoginView.fxml");
    }
    public nepsFeedback set(nepsFeedback n){
        nepsf=n;
        return nepsf;
    }
    public void back() throws IOException {
        FXMLLoader fxmlLoader=new FXMLLoader();
        URL url=getClass().getResource("../view/publicAqiDataList.fxml");
        fxmlLoader.setLocation(url);
        Parent root = fxmlLoader.load();
        Scene scene=new Scene(root);
        Stage stage = (Stage)back.getScene().getWindow();
        stage.setScene(scene);
        stage.show();
    }
    public void setNepsFeedback(nepsFeedback feedback) {
        if (feedback != null) {
            txt_address.setText(feedback.getProvince()+"  "+feedback.getCity()+"  "+feedback.getAddress());
            txt_content.setText(feedback.getContent());
            txt_level.setText(feedback.getLevel());
            txt_name.setText(feedback.getUsername());
            txt_number.setText(Integer.toString(feedback.getNumber()));
            txt_phonenumber.setText(feedback.getPhoneNumber());
            txt_date.setText(feedback.getDate());
            nepsf=feedback;
        }
    }
    public void localAssign() throws IOException {
        FXMLLoader fxmlLoader=new FXMLLoader();
        URL url =getClass().getResource("../view/LocalAssignView.fxml");
        fxmlLoader.setLocation(url);
        Parent root=fxmlLoader.load();
        Stage dialog=new Stage();
        dialog.setTitle("本地指派");
        dialog.initModality(Modality.APPLICATION_MODAL);
        Scene scene=new Scene(root);
        dialog.setScene(scene);
        dialog.showAndWait();
    }

    public  void assigna() throws IOException {
       nepsf.setID("1");
       Map<String, List<nepsFeedback>> fdMap=new HashMap<>();
       fdMap=readMapObject("NEPGworks.txt");
       if (fdMap.get("1") == null){
           fdMap.put("1", new ArrayList<>());
       }
       lista=fdMap.get("1");
       lista.add(nepsf);
       fdMap.put("1",lista);
       AlertUtils.showDialog("指派成功","反馈编号"+nepsf.getID()+"网格员ID：1，张三");
       writeMapObject("NEPGworks.txt",fdMap);
       System.out.println("指派成功");

    }
    public  void assignb() throws IOException {
        nepsf.setID("2");
        Map<String, List<nepsFeedback>> fdMap=new HashMap<>();
        fdMap=readMapObject("NEPGworks.txt");
        if (fdMap.get("2") == null){
            fdMap.put("2", new ArrayList<>());
        }
        listb=fdMap.get("2");
        listb.add(nepsf);
        fdMap.put("2",listb);
        AlertUtils.showDialog("指派成功","反馈编号"+nepsf.getID()+"网格员ID：2，李四");
        writeMapObject("NEPGworks.txt",fdMap);
        System.out.println("指派成功");
    }
    public  void assignc() throws IOException {
        nepsf.setID("3");
        Map<String, List<nepsFeedback>> fdMap=new HashMap<>();
        fdMap=readMapObject("NEPGworks.txt");
        if (fdMap.get("3") == null){
            fdMap.put("3", new ArrayList<>());
        }
        listc=fdMap.get("3");
        listc.add(nepsf);
        fdMap.put("3",listc);
        AlertUtils.showDialog("指派成功","反馈编号"+nepsf.getID()+"网格员ID：3，王五");
        writeMapObject("NEPGworks.txt",fdMap);
        System.out.println("指派成功");
    }
    public  void assignd() throws IOException {
        nepsf.setID("4");
        Map<String, List<nepsFeedback>> fdMap=new HashMap<>();
        fdMap=readMapObject("NEPGworks.txt");
        if (fdMap.get("4") == null){
            fdMap.put("4", new ArrayList<>());
        }
        listd=fdMap.get("4");
        listd.add(nepsf);
        fdMap.put("4",listd);
        AlertUtils.showDialog("指派成功","反馈编号"+nepsf.getID()+"网格员ID：4，赵六");
        writeMapObject("NEPGworks.txt",fdMap);
        System.out.println("指派成功");
    }
    public  void assigne() throws IOException {
        nepsf.setID("5");
        Map<String, List<nepsFeedback>> fdMap=new HashMap<>();
        fdMap=readMapObject("NEPGworks.txt");
        if (fdMap.get("5") == null){
            fdMap.put("5", new ArrayList<>());
        }
        liste=fdMap.get("5");
        liste.add(nepsf);
        fdMap.put("5",liste);
        AlertUtils.showDialog("指派成功","反馈编号"+nepsf.getID()+"网格员ID：5，诸葛亮");
        writeMapObject("NEPGworks.txt",fdMap);
        System.out.println("指派成功");
    }

}


